package com.mr.cai.main.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.mr.cai.main.R;

public class NaviFragmentFactory {
	public enum TITLE_ENUM{
		HOME_ENUM,//首页
		GROUP_ENUM,  //分类
		SHOPPINGCART_ENUM, //购物车
		USERINFO_ENUM //我的
	}
	private final static String TAG = NaviFragmentFactory.class.getSimpleName();
	private TITLE_ENUM currItem = TITLE_ENUM.HOME_ENUM ;
	private HomePageFragment mHomePageFragment;//首页
	private GroupPageFragment mGroupFragment; //分类
	private CartPageFragment mShoppingCartFragment; //购物车
	private MyPageFragment mUserFragment;//我的

	public NaviFragmentFactory () {
	}
	public TITLE_ENUM getCurrentItem(){
		return currItem;
	}


	public Fragment getCurrentItemFg(){
		return getItem(currItem);
	}
	public int getTitleEnumNum(TITLE_ENUM item){
		switch (item){
			case HOME_ENUM:
				return 0;
			case GROUP_ENUM:
				return  1;
			case SHOPPINGCART_ENUM:
				return 2;
			case USERINFO_ENUM:
				return 3;
			default:
				return 0;
		}
	}

	public Fragment getItem( TITLE_ENUM item){
		switch (item){
			case HOME_ENUM:
				return mHomePageFragment;
			case GROUP_ENUM:
				return  mGroupFragment;
			case SHOPPINGCART_ENUM:
				return mGroupFragment;
			case USERINFO_ENUM:
				return mUserFragment;
			default:
				return mHomePageFragment;
		}

	}

	public void hideAndShowFragment(FragmentManager fragmentManager, TITLE_ENUM index){
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.addToBackStack(null);
		currItem = index;
		switch (currItem) {
			case HOME_ENUM:
				if (mHomePageFragment == null) {
					mHomePageFragment = new HomePageFragment();
				}
				transaction.replace(R.id.content_main, mHomePageFragment);
				break;
			case GROUP_ENUM:
				if (mGroupFragment == null) {
					mGroupFragment = new GroupPageFragment();
				}
				transaction.replace(R.id.content_main, mGroupFragment);
				break;
			case SHOPPINGCART_ENUM:
				if (mShoppingCartFragment == null) {
					mShoppingCartFragment = new CartPageFragment();
				}
				transaction.replace(R.id.content_main, mShoppingCartFragment);
				break;
			case USERINFO_ENUM:
				if (mUserFragment == null) {
					mUserFragment = new MyPageFragment();
				}
				transaction.replace(R.id.content_main, mUserFragment);
				break;
		}
		transaction.commit();
	}


}
